package com.henanuniversity.dao.impl;

import com.henanuniversity.dao.DeliveryAddressDao;
import com.henanuniversity.po.Business;
import com.henanuniversity.po.DeliveryAddress;
import com.henanuniversity.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressDaoImpl implements DeliveryAddressDao {
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
        List<DeliveryAddress> list = new ArrayList<>();
        String sql="select * from deliveryaddress where userId=? order by daId";
        try {
            conn= DBUtils.getConnection();
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,userId);
            ResultSet rs = pst.executeQuery();
            while(rs.next()){
                DeliveryAddress address = new DeliveryAddress();
                address.setDaId(rs.getInt(1));
                address.setContactName(rs.getString(2));
                address.setContactSex(rs.getInt(3));
                address.setContactTel(rs.getString(4));
                address.setAddress(rs.getString(5));
                address.setUserId(rs.getString(6));
                list.add(address);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
             DBUtils.close(rs,pst);
        }
        return list;
    }

    @Override
    public int saveDeliveryAddress(DeliveryAddress deliveryAddress) {
        int result=0;
        String sql="insert into deliveryaddress values(null,?,?,?,?,?)";
        try {
            conn = DBUtils.getConnection();
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,deliveryAddress.getContactName());
            System.out.println(deliveryAddress.getContactSex());
            pst.setInt(2,0);
            pst.setString(3,deliveryAddress.getContactTel());
            pst.setString(4,deliveryAddress.getAddress());
            pst.setString(5,deliveryAddress.getUserId());
            result= pst.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
             DBUtils.close(rs,pst);
        }
        return result;
    }

    @Override
    public int removeDeliveryAddress(Integer daId) {
        int result=0;
        String sql="delete from deliveryaddress where daId=?";
        try {
            conn=DBUtils.getConnection();
            pst=conn.prepareStatement(sql);
            pst.setInt(1,daId);
           result= pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
             DBUtils.close(rs,pst);
        }

        return result;
    }

    @Override
    public DeliveryAddress getDeliveryAddressById(Integer daId) {
        ResultSet rs=null;
        DeliveryAddress deliveryAddress=null;
        String sql="select * from deliveryaddress where daId=?";

        try {
            conn=DBUtils.getConnection();
            pst=conn.prepareStatement(sql);
            pst.setInt(1,daId);
            rs=  pst.executeQuery();
            if(rs.next()){
                deliveryAddress=new DeliveryAddress();
                deliveryAddress.setDaId(rs.getInt(1));
                deliveryAddress.setContactName(rs.getString(2));
                deliveryAddress.setContactSex(rs.getInt(3));
                deliveryAddress.setContactTel(rs.getString(4));
                deliveryAddress.setAddress(rs.getString(5));
                deliveryAddress.setUserId(rs.getString(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(rs,pst);
        }


        return deliveryAddress;
    }

    @Override
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress) {
        int result=0;
        String sql="update deliveryaddress set contactName=?,contactSex=?,contactTel=?,address=?,userId=? where daId=?";
        try {
            conn=DBUtils.getConnection();
            pst=conn.prepareStatement(sql);
            pst.setString(1,deliveryAddress.getContactName());
            pst.setInt(2,deliveryAddress.getContactSex());
            pst.setString(3,deliveryAddress.getContactTel());
            pst.setString(4,deliveryAddress.getAddress());
            pst.setString(5,deliveryAddress.getUserId());
            pst.setInt(6,deliveryAddress.getDaId());
            result=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(rs,pst);
        }
        return result;
    }
}
